package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.OrderDetailManage;
import com.sjm.suchbear.minibookmall.action.OrderManage;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.model.OrderDetailModel;
import com.sjm.suchbear.minibookmall.model.OrderModel;
import com.sjm.suchbear.minibookmall.model.OrderState;
import com.sjm.suchbear.minibookmall.model.UserModel;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.tv_orderstate)
    TextView tvOrderstate;
    @BindView(R.id.tv_totalPrice)
    TextView tvTotalPrice;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.lv_orderShow)
    MyListView lvOrderShow;
    @BindView(R.id.tv_button)
    TextView tvButton;
    @BindView(R.id.rl_button)
    RelativeLayout rlButton;

    private String orderID;
    private OrderModel morderModel;
    private SQLiteDatabase dbread;
    private List<OrderDetailModel> orderDetailModels;
    private UserModel userModel;
    private OrderDetailAdapter orderDetailAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        tvListTitle.setText("订单详情");
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
        orderID = getIntent().getStringExtra("orderID");
        if (orderID == null) {
            T.showShort(getBaseContext(), "订单号错误");
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        morderModel = OrderManage.getOrderListbyorderid(dbread, orderID);
        orderDetailModels = OrderDetailManage.getOrderDetailByid(dbread, orderID);
        userModel = UserManage.findUserByID(dbread, SP.getUid(getBaseContext()));
        tvName.setText("收货人:       ".concat(morderModel.getOrdername()));
        tvPhone.setText("手机号:       ".concat(morderModel.getOrderphone()));
        tvAddress.setText("收货地址:    ".concat(morderModel.getOrderaddress()));
        tvTotalPrice.setText("订单总价:   ¥".concat(morderModel.getOrderprice()));
        if (morderModel.getOrderstate().equals(String.valueOf(OrderState.WAITPAY))) {
            tvOrderstate.setText("订单状态:   " + "待付款");
            if (userModel.getAuth() == 0) {
                rlButton.setVisibility(View.VISIBLE);
                tvButton.setText("支付订单");
            }
        } else {
            tvOrderstate.setText("订单状态:   " + "待发货");
            if (userModel.getAuth() == 0) {
                rlButton.setVisibility(View.GONE);
            }
        }
        if (userModel.getAuth() == 1) {
            rlButton.setVisibility(View.VISIBLE);
            tvButton.setText("取消订单");
            ivHeaderFunctionIco.setImageResource(R.drawable.img_edit);
        }

        orderDetailAdapter = new OrderDetailAdapter(getBaseContext(), orderDetailModels);
        lvOrderShow.setAdapter(orderDetailAdapter);
    }

    @OnClick({R.id.ll_header_back, R.id.ll_header_function, R.id.tv_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                finish();
                break;
            case R.id.ll_header_function:
                if (userModel.getAuth() == 1) {
                    //跳转修改订单信息
                    Intent intent = new Intent(OrderDetailActivity.this, EditInfoActivity.class);
                    intent.putExtra("mode", 1);
                    intent.putExtra("orderID", orderID);
                    startActivity(intent);
                }
                break;
            case R.id.tv_button:
                if (userModel.getAuth() == 0) {
                    BigDecimal orderprice = new BigDecimal(morderModel.getOrderprice());
                    BigDecimal mcost = new BigDecimal(userModel.getCost());
                    BigDecimal result = mcost.subtract(orderprice);
                    if (result.compareTo(new BigDecimal(0)) == -1) {
                        T.showShort(getBaseContext(), "支付失败，余额不足");
                    } else {
                        UserManage.changeUserCost(dbread, userModel.getUserID(), result.toString());  //减少余额
                        OrderManage.changeOrderState(dbread, orderID, OrderState.HADPAY); //修改支付状态
                        T.showShort(getBaseContext(), "支付成功");
                        initData();
                    }
                } else if (userModel.getAuth() == 1) {
                    OrderManage.deleteOrder(dbread, orderID);
                    T.showShort(getBaseContext(), "取消成功");
                    finish();
                }
                break;
        }
    }
}
